<?php

namespace App\Console\Commands;
use App\Models\Animal;
use App\Models\ArticleRelateCircle;
use App\Models\Circle;
use App\Models\Subscribe;
use App\Models\UserExtend;
use App\Models\UserJoinCircle;
use App\Services\PublishTemplateService;
use Illuminate\Console\Command;

class BirthdayCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'animal-birthday';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生日提醒';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Animal::where('birthday_md', date('m-d'))->withCertain('user', ['openid'])->chunkById(100, function ($list) {
            foreach ($list as $item) {
                $exists = Subscribe::where('user_id', $item->user_id)->where('type', Subscribe::TYPE_BIRTHDAY)->exists();
                if ($exists) {
                    $service = new PublishTemplateService();
                    $service->birthday($item->user->openid, $item->title);
                    Subscribe::where('user_id', $item->user_id)->where('type', Subscribe::TYPE_BIRTHDAY)->delete();
                }
            }
        });
    }
}
